//
// Created by JYL on 2023-12-20.
//
#include "debug_task.h"
#include "task.h"
#include "mpu6050.h"
#include "beep.h"


/* 后台程序 */
uint8_t exit_flag = 0;
uint8_t rising_falling_flag;

static void buzzer_warn_error(uint8_t num);//buzzer_warn_error(50);

//注意1: 按键按下是高电平
//注意2: os_delay之后，任务进程能拿回来(优先级要高)
void Debug_Task(void *argument)
{
    uint8_t CPU_RunInfo[700];
    /* Infinite loop */
    for(;;)     //前台程序
    {
        if(exit_flag == 1){
            exit_flag = 2;
            if(rising_falling_flag == GPIO_PIN_SET){    //按下
                osDelay(20);//HAL_Delay(20);
                if(KEY() == GPIO_PIN_SET){
                    LED1_TOGGLE();
//                    memset(CPU_RunInfo,0,700); //信息缓冲区清零//printf("\r\n任务名   任务状态   优先级   剩余栈   任务序号\r\n");
                    vTaskGetRunTimeStats((char *)&CPU_RunInfo);
                    printf("%s\r\n",CPU_RunInfo);
                    printf("---------------------------------------------\r\n\n");
                } else exit_flag = 0;
            }else if(rising_falling_flag == GPIO_PIN_RESET){    //松手
                osDelay(20);//HAL_Delay(20);
                if(KEY() == GPIO_PIN_RESET){
                    LED2_TOGGLE();
//                    memset(CPU_RunInfo,0,700);printf("\r\ntaskname\truntime\t运行所占百分比\r\n");//printf("%s\r\n",InfoBuffer);
                    vTaskList((char *)&CPU_RunInfo); //获取任务运行时间信息
                    printf("%s\r\n",CPU_RunInfo);
                    printf("---------------------------------------------\r\n\n");
                } else exit_flag = 0;
            }
        }else if(exit_flag == 2)exit_flag=0;
        osDelay(20);
    }
}

/**
  * @brief          使得蜂鸣器响
  * @param[in]      num:响声次数
  * @retval         none
  */
static void buzzer_warn_error(uint8_t num)
{
    static uint8_t show_num = 0;
    static uint8_t stop_num = 100;
    if(show_num == 0 && stop_num == 0){
        show_num = num;
        stop_num = 100;
    }
    else if(show_num == 0){
        stop_num--;
        Buzzer(0);
    }else{
        static uint8_t tick = 0;
        tick++;
        if(tick < 50)Buzzer(0);
        else if(tick < 100)Buzzer(1);
        else{
            tick = 0;
            show_num--;
        }
    }
}
